package com.lonely.fx.tree_view;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.control.cell.TextFieldTreeCell;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.util.Optional;

/**
 * @date: 2022-08-03 15:56
 * @author: ztkj-hzb
 * @description: treeview 事件设置
 **/
public class TreeViewWithEventSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {


        AnchorPane anchorPane = new AnchorPane();

        Button button = new Button("触发事件动作");


        //创建TreeView
        TreeView<String> treeView = new TreeView<>();

        //准备数据

        //创建根节点
        TreeItem<String> root = new TreeItem<>("中国");

        TreeItem<String> hb = new TreeItem<>("湖北省");
        TreeItem<String> hb_1 = new TreeItem<>("鄂州市");
        TreeItem<String> hb_2 = new TreeItem<>("武汉市");
        TreeItem<String> hb_3 = new TreeItem<>("襄樊市");
        TreeItem<String> hb_4 = new TreeItem<>("黄冈市");

        //添加子节点
        hb.getChildren().addAll(hb_1, hb_2, hb_3, hb_4);

        TreeItem<String> gd = new TreeItem<>("广东省");
        TreeItem<String> gd_1 = new TreeItem<>("广州市");
        TreeItem<String> gd_2 = new TreeItem<>("深圳市");
        TreeItem<String> gd_3 = new TreeItem<>("东莞市");
        gd.getChildren().addAll(gd_1, gd_2, gd_3);

        TreeItem<String> hn = new TreeItem<>("湖南省");
        TreeItem<String> hn_1 = new TreeItem<>("长沙市");
        TreeItem<String> hn_2 = new TreeItem<>("岳阳市");
        hn.getChildren().addAll(hn_1, hn_2);

        root.getChildren().addAll(hb, gd, hn);

        //设置根节点
        treeView.setRoot(root);
        root.setExpanded(true);

        //设置宽高
        treeView.setPrefSize(400, 400);


        //设置可编辑
        treeView.setEditable(true);
        //设置cellfactory--文本框
        treeView.setCellFactory(TextFieldTreeCell.forTreeView());

        //选择指定treeitem时候触发
        /*treeView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TreeItem<String>>() {
            @Override
            public void changed(ObservableValue<? extends TreeItem<String>> observable, TreeItem<String> oldValue, TreeItem<String> newValue) {
                System.out.println(newValue.getValue());
            }
        });*/

        //TODO 指定节点添加事件

        //TODO：注意 由于事件传播机制，在root节点上设置事件，里面所有的节点对于该事件的触发都会执行到这里，所以如果想给所有节点添加同一个事件，直接在root节点上添加即可
        root.addEventHandler(TreeItem.valueChangedEvent(), new EventHandler<TreeItem.TreeModificationEvent<String>>() {
            @Override
            public void handle(TreeItem.TreeModificationEvent<String> event) {
                System.out.println("valueChange:" + event.getTreeItem().getValue());
            }
        });

        //指定treeitem添加 value变更事件触发---只在改节点触发
        /*hb_1.addEventHandler(TreeItem.valueChangedEvent(), new EventHandler<TreeItem.TreeModificationEvent<String>>() {
            @Override
            public void handle(TreeItem.TreeModificationEvent<String> event) {
                System.out.println("hb1_source:"+event.getSource().getValue());
                System.out.println("hb1_newValue:"+event.getNewValue());
                System.out.println("hb1_treeItem:"+event.getTreeItem().getValue());
            }
        });*/


        //graphicChangedEvent事件
        root.addEventHandler(TreeItem.graphicChangedEvent(), new EventHandler<TreeItem.TreeModificationEvent<Object>>() {
            @Override
            public void handle(TreeItem.TreeModificationEvent<Object> event) {
                //获取修改graphic的item对象
                System.out.println("graphicChange:" + event.getTreeItem().getValue());
            }
        });

        // item选项展开触发事件
        root.addEventHandler(TreeItem.branchExpandedEvent(), new EventHandler<TreeItem.TreeModificationEvent<String>>() {
            @Override
            public void handle(TreeItem.TreeModificationEvent<String> event) {
                System.out.println("branchExpanded:" + event.getTreeItem().getValue());
            }
        });

        // item选项收缩触发事件
        root.addEventHandler(TreeItem.branchCollapsedEvent(), new EventHandler<TreeItem.TreeModificationEvent<String>>() {
            @Override
            public void handle(TreeItem.TreeModificationEvent<String> event) {
                System.out.println("branchCollapsed:" + event.getTreeItem().getValue());
            }
        });

        // 对于 treeitem集合长度变更时触发
        root.addEventHandler(TreeItem.childrenModificationEvent(), new EventHandler<TreeItem.TreeModificationEvent<String>>() {
            @Override
            public void handle(TreeItem.TreeModificationEvent<String> event) {
                System.out.println("childenModification:" + event.getTreeItem().getValue());
            }
        });

        //可见item数量的变化触发事件
        root.addEventHandler(TreeItem.expandedItemCountChangeEvent(), new EventHandler<TreeItem.TreeModificationEvent<String>>() {
            @Override
            public void handle(TreeItem.TreeModificationEvent<String> event) {
                System.out.println("expandedItemCountChange:" + event.getTreeItem().getValue());
            }
        });


        //关于 treeitem的事件最终都会执行到这里，根据eventtype区分处理
        root.addEventHandler(TreeItem.treeNotificationEvent(), new EventHandler<TreeItem.TreeModificationEvent<String>>() {
            @Override
            public void handle(TreeItem.TreeModificationEvent<String> event) {
                System.out.println("treeNotification:" + event.getEventType());
            }
        });


        anchorPane.getChildren().addAll(button, treeView);
        AnchorPane.setTopAnchor(treeView, 100.0);
        primaryStage.setScene(new Scene(anchorPane));
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);
        primaryStage.show();


        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                //设置graphic--触发 GraphicChangedEvent事件
                //root.setGraphic(new ImageView(new Image("icons/lbxx.png", 30, 30, true, true)));

                //修改子列表--触发 childenModification事件
                //root.getChildren().get(1).getChildren().add(new TreeItem<>("佛山"));

                //修改值：触发 ValueChangedEvent事件
                //hb_1.setValue("大鄂州");

                //展开选项--触发 branchExpanded 事件
                //expandOrShrink(root, true);

                expandOrShrink(root, false);
            }
        });

    }


    private void expandOrShrink(TreeItem<String> treeItem, boolean isExpand) {

        //如果是叶子节点，直接返回
        if (treeItem.isLeaf()) {
            return;
        }

        //非叶子节点,设置状态
        treeItem.setExpanded(isExpand);

        //获取下级节点
        for (TreeItem<String> child : treeItem.getChildren()) {
            expandOrShrink(child, isExpand);
        }

    }
}
